//
//  AddItemViewController.m
//  CheckList
//
//  Created by chen on 15/8/4.
//  Copyright (c) 2015年 ZhangXM. All rights reserved.
//

#import "AddItemViewController.h"
#import "CheckListItem.h"

@interface AddItemViewController ()

@end

@implementation AddItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.itemToEdit != nil) {
        self.title = @"EditItem";
        self.textField.text = self.itemToEdit.text;
        self.doneBarButton.enabled = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//cell选中不变灰
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

//键盘
- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.textField becomeFirstResponder];
}
//判断cell的内容是否为空，确定Done的Enable；
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (newText.length > 0 ) {
        self.doneBarButton.enabled = YES;
    }else{
        self.doneBarButton.enabled = NO;
    }
    return YES;
}
//取消建
- (IBAction)cancelBarButton:(UIBarButtonItem *)sender {
    [self.delegate addItemViewControllerDidCancel:self];
}
//done健
- (IBAction)doneBarButton:(UIBarButtonItem *)sender {
    if(self.itemToEdit == nil){
        CheckListItem *item = [[CheckListItem alloc]init];
        item.text = self.textField.text;
        item.Checked = NO;
        
        [self.delegate addItemViewController:self didFinishAddingItem:item];
    }else{
        self.itemToEdit.text = self.textField.text;
        [self.delegate addItemViewController:self didFinishEditingItem:self.itemToEdit];
    }
    
}
@end
